﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using static Models.Common.EnumDeviceState;
using GatewayServerService.Data;
using UtilsORM.MySqlData;
using UtilsBase;
using GatewayServerService.CloudCenter;
using Microsoft.EntityFrameworkCore;
using NewLife.Log;
using UtilsORM;
using UtilsCache;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Xml.Linq;

namespace GatewayServerService.CloudCenter
{
    /// <summary>
    /// 写入设备值，统一在这里写入
    /// </summary>
    public class ServiceCloudDeviceValueLog
    {

        /// <summary>
        /// 写入传感器数据值
        /// </summary>
        /// <param name="dguid"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void writeSensorlog(string dguid, string key, string value, string name, string remark = "")
        {
            //这里还可以进行终极优化，如果判断值和上次一样，没有变，可以不写入
            try
            {
                EFYihongIotContext ef = new EFYihongIotContext();

                int count = ef.iot_device_value_current.Where(c => c.vc_d_guid == dguid && c.vc_key == key).ExecuteUpdate(c => c
                  .SetProperty(p => p.vc_key, key)
                  .SetProperty(p => p.vc_value, value)
                  .SetProperty(p => p.vc_name, name)
                  .SetProperty(p => p.vc_remark, remark)
                  .SetProperty(p => p.vc_add_time, DateTime.Now));

                //写入设备当前值
                if (count < 1)
                {
                    iot_device_value_current iot_Device_Value_Current = new iot_device_value_current();
                    iot_Device_Value_Current.vc_guid = Guid.NewGuid().ToString();
                    iot_Device_Value_Current.vc_d_guid = dguid;
                    iot_Device_Value_Current.vc_value = value;
                    iot_Device_Value_Current.vc_key = key;
                    iot_Device_Value_Current.vc_add_time = DateTime.Now;
                    iot_Device_Value_Current.vc_name = name;
                    iot_Device_Value_Current.vc_remark = remark;
                    ef.iot_device_value_current.Add(iot_Device_Value_Current);
                    ef.SaveChanges();
                }

                // 把最新值，添加到缓存
                //写入缓存
                UtilMemoryCacheHelper.set("deviceKey_" + dguid + key, value, 60);

                writeLogSync(dguid, key, value, name, remark);

                //推送到云远程
                new ServiceCloudRemoteManager().sendMsgDeviceStatusChange(dguid, key, value);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }


        /// <summary>
        /// 写数据日志
        /// </summary>
        /// <param name="dguid"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="name"></param>
        /// <param name="remark"></param>
        public void writeLogSync(string dguid, string key, string value, string name, string remark = "")
        {



            EFYihongIotContext ef = new EFYihongIotContext();

            //更新设备状态，设备状态
            iot_device_value_log addlog = new iot_device_value_log();
            addlog.vl_guid = Guid.NewGuid().ToString();
            addlog.vl_d_guid = dguid;
            addlog.vl_key = key;
            addlog.vl_value = value;
            addlog.vl_name = name;
            addlog.vl_remark = remark;
            addlog.vl_add_time = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            ef.iot_device_value_log.Add(addlog);
            ef.SaveChanges();


            //判断报警信息
            iot_device_model model = new DBIotDeviceModelCache().getModelByDeviceCache(dguid);
            if (model.m_code == "1039")
            {
                //写入报警记录
                new DBIotWarnLog().addWarnLogSync(dguid, key, value, name, remark);
            }

        }

    }

}
